<!-- 报表页 -->
<template>
  <el-form :model="valueForm"  >
    <div class="statementSelect">
      <el-date-picker
            v-model="dateValue"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="large"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      <!-- 添加者下拉框 -->
      <span>添加者
        <el-select v-model="valueForm.userList" multiple default-first-option :reserve-keyword="false"
          placeholder="请选择添加者">
          <el-option v-for="item in optionsAdder" :key="item" :label="iteme" :value="item" />
        </el-select>
      </span>
      <!-- 类型下拉框 -->
      <span>类型
        <el-select v-model="valueForm.typeList" multiple default-first-option :reserve-keyword="false"
          placeholder="请选择类型">
          <el-option label="收入" value="0" key="0" />
          <el-option label="支出" value="1" key="1" />
        </el-select>
      </span>
      <el-button class="search" icon="Search" @click="getRevExpByCondition()">搜索</el-button>
      <el-button class="download" icon="Download" @click="downloadStatement()">下载</el-button>
    </div>
  </el-form>
  <hr style="background-color: #e8e8e8; height: 2px; border: none" />
  <!-- 表格 -->
  <el-table :data="revExpInfo.revExpList" stripe style="width: 100%;">
    <el-table-column prop="adder" label="添加者" />
    <el-table-column prop="money" label="金额" />
    <el-table-column prop="typeName" label="类型名称" />
    <el-table-column prop="date" label="日期" />
    <el-table-column prop="describes" label="描述" />
  </el-table>
  <div class="statistic">
    <el-row>
      <el-col :span="8">
        <el-statistic title="总收入" :value="revExpInfo.income" />
      </el-col>
      <el-col :span="8">
        <el-statistic title="总支出" :value="revExpInfo.expend" />
      </el-col>
      <el-col :span="8">
        <el-statistic title="总 计" :value="revExpInfo.total" />
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { saveAs } from 'file-saver';
import { getNameList } from '@/api/user';
import { getByCondition } from '@/api/revexp';
import { statement } from '@/api/download';

const revExpInfo = ref([]);
const dateValue = ref('');
//搜索表单数据
const valueForm = reactive({
  userList: [],
  typeList: [],
});
//用户数据列表
const optionsAdder = ref([]);

const getUsersName = () =>{
  getNameList().then((res) => {
    optionsAdder.value = res.data;
  })
}

const getRevExpByCondition = () => {
  if(dateValue.value != '') {
    dateValue.value[1] = dateValue.value[1].replace('00:00:00','23:59:59');
  }
  getByCondition(JSON.stringify(dateValue.value),JSON.stringify(valueForm.userList),JSON.stringify(valueForm.typeList)).then((res) => {
    revExpInfo.value = res.data;
  });
}

const downloadStatement = () => {
  if(dateValue.value != '') {
    dateValue.value[1] = dateValue.value[1].replace('00:00:00','23:59:59');
  }
  statement(JSON.stringify(dateValue.value),JSON.stringify(valueForm.userList),JSON.stringify(valueForm.typeList)).then((res) => {
    let filename = res.headers["content-disposition"].split("attachment;filename*=utf-8''")[1];
    saveAs(res.data, decodeURIComponent(filename));
  })
}

onMounted(() => {
  getUsersName();
});
</script>

<style>
.el-range-editor--large.el-input__wrapper {
  margin-right: 20px;
}

.statementSelect span {
  font-size: 14px;
}

.statementSelect .el-select .el-input__wrapper {
  margin-right: 20px;
}

.statementSelect .search {
  width: 80px;
  margin-right: 50px;
}

.el-table {
  /* font-size: 12px; */
  --el-table-header-bg-color: #a1a3a650;
}

.el-table .cell {
  text-align: center;
}

.el-table__inner-wrapper {
    width: 100%;
    height: 69vh;
}
</style>